package com.bls.executor.word.noun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bls.action.BLSAction;
import com.bls.constant.ResultCodeEnum;
import com.bls.exception.MyException;
import com.bls.remote.service.CoreRemoteService;
import com.bls.utils.BandCore;

import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.Map;

public class Tool extends Noun {

    CoreRemoteService coreRemoteService = null;

    {
        try {
            coreRemoteService = BLSAction.getBean(CoreRemoteService.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public String bandId ="";
    public String toolId;
    public String text;

    public Tool(String text, Map<String, Object> scriptEnvironment, String bandId) throws MyException {
        this.bandId = bandId;
        this.text = text;
        init(text, scriptEnvironment);
    }

    public Tool(String text, Map<String, Object> scriptEnvironment) throws MyException {
        init(text, scriptEnvironment);
    }

    private void init(String text, Map<String, Object> scriptEnvironment) throws MyException {
        if (text.startsWith("\"")) {
            text = text.substring(1, text.length() - 1);
        } else {
            text = text.trim();
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = coreRemoteService.getToolByToolName(text,this.bandId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (jsonObject == null || ((int) jsonObject.get("total")) == 0) {
            throw new MyException(ResultCodeEnum.RUN_ERROR_TOOLNAME.getCode(), ResultCodeEnum.RUN_ERROR_TOOLNAME.getMsg());
        }
        JSONArray rows = jsonObject.getJSONArray("rows");
        String toolID = (String) rows.getJSONObject(0).get("toolID");
        this.toolId = toolID;
    }

    @Override
    public boolean run(String methodName) {
        return super.run(methodName);
    }

    public static void main(String[] args) throws Exception {
        String toolName = "帮语平台";
        String bandID = "314545119";
        String query = "{\"name\":\"%s\",\"bandID\":\"%s\"}";
        query = String.format(query, toolName, bandID);

        String encode = URLEncoder.encode(query, "UTF-8");
        String httpurl = "https://www.wetoband.com/core//core/v4/tool?format=json&gid=%s&query=%s&aid=21419389378723787447";
//        String accessToken = BandCore.getAccessToken();
        String accessToken = "998f809f7a809568e1217fb039be6ec0217d803b6ce72a80e954ecf9088a9e6c65cd11c230e3d115f4da55a1540129d3d534d87ac23eda8e4033c04e981aeae2";
        httpurl = String.format(httpurl, "314545119", encode);
        String s = BandCore.doGetWithAccessToken(httpurl, accessToken);
        System.out.println(s);
        JSONObject jsonObject = JSON.parseObject(s);
        if (((int) jsonObject.get("total")) == 0) {
            throw new RuntimeException("查不到该帮区信息：" + toolName);
        }
        JSONArray rows = jsonObject.getJSONArray("rows");
        String toolID = (String) rows.getJSONObject(0).get("toolID");
        System.out.println("toolID = " + toolID);
    }
}
